"use strict";

var _get = require("./utils/get");

const { exec } = require("child_process");

// var _getRename = require("./utils/custom_pro");
// var _fileUtils = require("./utils/fileUtils");

var _ora = require("ora");

var _ora2 = _interopRequireDefault(_ora);

var _inquirer = require("inquirer");

var _inquirer2 = _interopRequireDefault(_inquirer);

var _fs = require("fs");

var _fs2 = _interopRequireDefault(_fs);

var _chalk = require("chalk");

var _chalk2 = _interopRequireDefault(_chalk);

var _logSymbols = require("log-symbols");

var _logSymbols2 = _interopRequireDefault(_logSymbols);

var _rename = require("./rename");

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

let init = async (projectName) => {
  //项目不存在
  if (!_fs2.default.existsSync(projectName)) {
    if (
      !projectName ||
      projectName === undefined ||
      projectName === "undefined"
    ) {
      console.log(
        _logSymbols2.default.error,
        _chalk2.default.red("项目名不能为空！")
      );
      return true;
    }
    let list = ["web", "h5", "RN ++", "RN@0.57.8"];
    let result = await _inquirer2.default.prompt([
      {
        type: "list",
        name: "project",
        choices: list,
        questions: "选择要生成的模板",
      },
    ]);
    //项目名字
    let project = result.project;
    let templateName = "RN_0.57.8";
    switch (project) {
      case "web":
        templateName = "web";
        break;
      case "h5":
        templateName = "h5";
        break;
      case "RN ++":
        templateName = "RN_0.61.5";
        break;
      case "RN@0.57.8":
        templateName = "RN_0.57.8";
        break;
      default:
        templateName = "RN_0.57.8";
        break;
    }
    console.log(templateName);
    //命令行交互
    _inquirer2.default
      .prompt([
        {
          name: "description",
          message: "请输入工程描述内容: ",
        },
        {
          name: "author",
          message: "请输入工程开发者: ",
        },
      ])
      .then(async (answer) => {
        //下载模板 选择模板
        //通过配置文件，获取模板信息

        let loading = (0, _ora2.default)("正在下载模板...");
        loading.start();
        (0, _get.downloadLocal)(templateName, projectName).then(
          () => {
            loading.succeed("下载完成😄");
            const fileName = `${projectName}/package.json`;
            if (_fs2.default.existsSync(fileName)) {
              const data = _fs2.default.readFileSync(fileName).toString();
              let json = JSON.parse(data);
              json.name = projectName;
              json.author = answer.author;
              json.description = answer.description;
              //修改项目文件夹中 package.json 文件
              _fs2.default.writeFileSync(
                fileName,
                JSON.stringify(json, null, "\t"),
                "utf-8"
              );
              // console.log(
              //   _logSymbols2.default.success,
              //   _chalk2.default.green("下载完成😄")
              // );
              // _chalk2.default.green("项目初始化搭建成功!");
              let loading2 = null;
              switch (project) {
                case "web":
                  templateName = "web";
                  break;
                case "RN ++":
                  templateName = "rnapp";
                  // console.log("RN ++");
                  // 用官网的去下载初始化工程
                  // _rename(templateName, projectName);
                  // 解耦
                  loading2 = (0, _ora2.default)("正在创建项目...");
                  loading2.start();

                  exec(
                    `react-cli rename ${templateName} ${projectName}`,
                    (error, stdout, stderr) => {
                      // console.log(error);
                      console.log(stdout, stderr);
                      loading2.succeed(`创建"${projectName}"项目完成😄`);
                    }
                  );
                  break;
                case "RN@0.57.8":
                  templateName = "rnapp";
                  loading2 = (0, _ora2.default)("正在创建项目...");
                  loading2.start();
                  exec(
                    `react-cli rename ${templateName} ${projectName}`,
                    (error, stdout, stderr) => {
                      console.log(stdout, stderr);
                      loading2.succeed(`创建"${projectName}"项目完成😄`);
                    }
                  );
                  // _rename(templateName, projectName);
                  break;
              }
            }
          },
          (err) => {
            console.log(
              _logSymbols2.default.error,
              _chalk2.default.red(
                `statusCode:${err.statusCode} ${err.statusMessage}`
              )
            );
          }
        );
      });
  } else {
    //项目已经存在
    console.log(
      _logSymbols2.default.error,
      _chalk2.default.red("项目已去存在")
    );
  }
};

module.exports = init;
